package com.wiki4j.gdo.impl;

import java.io.Reader;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.wiki4j.gdo.GDOType;
import com.wiki4j.gdo.GDObject;
import com.wiki4j.gdo.GDProperty;
import com.wiki4j.gdo.GDType;

public class GDOTypeImpl extends LinkedHashMap<String,GDProperty> implements GDOType{
  private GDOType parent;
  private String ns;
  private String name;

  public GDOTypeImpl(String ns,String name){
    this.ns=ns;
    this.name=name;
  }

  public GDOTypeImpl(GDOType parent){
    this.parent=parent;
  }

  public GDOTypeImpl(){
  }

  @Override
  public String namespace(){
    return ns;
  }

  @Override
  public String name(){
    return name;
  }

  public void namespace(String ns){
    this.ns=ns;
  }

  public void name(String name){
    this.name=name;
  }

  @Override
  public String getFullName(){
    return namespace()+"."+name();
  }

  @Override
  public boolean isContainer(){
    return true;
  }

  @Override
  public GDProperty getProperty(String propertyName){
    return get(propertyName);
  }

  @Override
  public Map<String,GDProperty> getProperties(){
    return this;
  }

  @Override
  public void setProperty(String propertyName,GDProperty property){
    put(propertyName,property);
  }

  @Override
  public void setProperty(String propertyName,GDType type){
    setProperty(propertyName,type,0);
  }

  @Override
  public void setProperty(String propertyName,GDType type,int arrayDim){
    GDProperty property=new GDPropertyImpl(propertyName,type,arrayDim);
    setProperty(propertyName,property);
  }

  @Override
  public GDOType addGDO(String propertyName){
    return addGDO(propertyName,0);
  }

  @Override
  public GDOType addGDO(String propertyName,int arrayDim){
    GDOType child=new GDOTypeImpl(this);
    setProperty(propertyName,child);
    return child;
  }

  @Override
  public GDObject toGDObject(Object src){
    return (GDObject)src;
  }

  @Override
  public Object fromGDObject(GDObject value){
    return value;
  }

  @Override
  public List<?> toList(Object src){
    throw new IllegalArgumentException("Not array!");
  }

  @Override
  public Object fromList(List<?> value){
    throw new IllegalArgumentException("Not array!");
  }

  @Override
  public GDObject create(){
    return new GDObjectImpl(this);
  }

  @Override
  public GDObject readGDO(String json){
//    GDObjectImpl gdo=JSON.parseObject(json,GDObjectImpl.class);
//    return gdo;
    return null;
  }

  @Override
  public GDObject read(Reader reader){
    GDObjectImpl gdo=new GDObjectImpl(this);
    return gdo;
  }

  @Override
  public String toJson(){
    return JSON.toJson(this);
  }

  @Override
  public String toString(){
    return toJson();
  }
}
